package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.mongo.VideoApi;
import com.tanhua.dubbo.utils.IdService;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;
    @Override
    public void save(Video video) {
        video.setId(new ObjectId());
        video.setCreated(new Date().getTime());
        video.setVid(idService.getNextId("video"));
        mongoTemplate.save(video);
    }

    @Override
    public PageResult querySmallVideos(Integer page, Integer pagesize) {

        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);

        return new PageResult(page,pagesize,(int)count,videoList);
    }

    @Override
    public PageResult queryVideos(Integer page, Integer pagesize, Long uid) {
        Query query = new Query(Criteria.where("userId").is(uid));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);

        return new PageResult(page,pagesize,(int)count,videoList);
    }

    //小视频点赞
    @Override
    public Integer saveVideo(Video video, Long userId) {

        ObjectId videoId = video.getId();
        Query query = new Query(Criteria.where("id").is(videoId));
        Update update = new Update();
        update.inc("likeCount",1);
        mongoTemplate.updateFirst(query, update, Video.class);
        Video video1 = mongoTemplate.findById(videoId, Video.class);

        Comment comment = new Comment();
        comment.setId(new ObjectId());
        comment.setPublishId(videoId);  //小视频id
        comment.setCommentType(4);  //评论类型 4 视频点赞
        comment.setPublishUserId(video1.getUserId());  //小视频的userid
        comment.setLikeCount(video1.getLikeCount());  //视频的点赞数
        comment.setUserId(userId);
        comment.setCreated(new Date().getTime());

        mongoTemplate.save(comment);

        return video1.getLikeCount();
    }

    //对小视频取消点赞
    @Override
    public Integer deleteVideo(Video video, Long userId) {

        ObjectId videoId = video.getId();
        Query query = new Query(Criteria.where("id").is(videoId));
        Update update = new Update();
        update.inc("likeCount",-1);
        mongoTemplate.updateFirst(query, update, Video.class);
        Video video1 = mongoTemplate.findById(videoId, Video.class);

        Comment comment = new Comment();
        comment.setId(new ObjectId());
        comment.setPublishId(videoId);  //小视频id
        comment.setCommentType(4);  //评论类型 4 视频点赞
        comment.setPublishUserId(video1.getUserId());  //小视频的userid
        comment.setLikeCount(video1.getLikeCount());  //视频的点赞数
        comment.setUserId(userId);
        comment.setCreated(new Date().getTime());
        mongoTemplate.save(comment);

        return video1.getLikeCount();
    }

    //分页查询小视频评论
    @Override
    public PageResult queryComments(String videoId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(videoId)).and("commentType").is(5));
        query.with(Sort.by(Sort.Direction.DESC,"created"));
        query.limit(pagesize).skip((page-1)*pagesize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }


}
